var assert = require('assert');
var logger = using('easynode.framework.Logger').forFile(__filename);
var GenericObject = using('easynode.GenericObject');
var BeanFactory = using('easynode.framework.BeanFactory');
//var wechat = require('co-wechat');

(function () {
        const ALLOW_METHOD = EasyNode.config('weixin.service.httpMethod', 'all');
        /**
         * Class Main
         *
         * @class #NAMESPACE#.Main
         * @extends easynode.GenericObject
         * @since 0.1.0
         * @author zlbbq
         * */
        class Main extends GenericObject {
                /**
                 * 构造函数。
                 *
                 * @method 构造函数
                 * @since 0.1.0
                 * @author zlbbq
                 * */
                constructor() {
                        super();
                        //调用super()后再定义子类成员。
                }

                static main() {
                        return function * () {
                                logger.info('initializing project [wxapp]...');
                                var koaHttpServer = BeanFactory.get('httpServer');
                                koaHttpServer.addWebDirs('projects/wxapp/sub-systems/web/www/');
                                EasyNode.addi18nDirectory('projects/wxapp/sub-systems/web/etc/i18n');
                                if(EasyNode.config('http.simulateLogin') === '1') {
                                        koaHttpServer.addMiddleware(function * (next) {
                                                if(this.session.USER == null) {
                                                        logger.info('simulate login');
                                                        this.session.USER = BeanFactory.get('simulatedLoginUser');
                                                }
                                                yield next;
                                        });
                                }

                            /*    koaHttpServer.addRoute(ALLOW_METHOD, '/weixin',  wechat('some token').middleware(function *() {
                                        // 微信输入信息都在this.weixin上
                                        var message = this.weixin;
                                        if (message.FromUserName === 'diaosi') {
                                                // 回复屌丝(普通回复)
                                                this.body = 'hehe';
                                        }
                                })
                                );*/
                       /*         koaHttpServer.addMiddleware(
                                        wechat('some token').middleware(function *() {
                                                // 微信输入信息都在this.weixin上
                                                var message = this.weixin;
                                                if (message.FromUserName === 'diaosi') {
                                                        // 回复屌丝(普通回复)
                                                        this.body = 'hehe';
                                                }
                                        })
                                )*/
                                yield koaHttpServer.start();
                        };
                }
                getClassName() {
                        return EasyNode.namespace(__filename);
                }
        }

        module.exports = Main;
})();